package gcommon

import (
	"time"

	"gitee.com/night-tc/gobige/global"
	"github.com/spf13/viper"
)

type ServiceConfig struct {
	*global.GlobalConfig
	// login的验证码
	LoginToken string
	// 用户缓存时间 (gateway,用户链接断开后设置定时器)
	UserCacheTime time.Duration
}

// 获取NewServer需要的参数
func LoadServerConfig(vmap *viper.Viper, sname string, stype global.ServerTypeEnum) (res *ServiceConfig) {
	res = &ServiceConfig{}
	res.GlobalConfig = global.LoadServerConfig(vmap, sname, stype)
	var (
		vcfg = viper.New() //config
		vsrv = viper.New() //对应服务器配置
	)
	_ = vcfg.MergeConfigMap(vmap.GetStringMap("Config"))
	_ = vsrv.MergeConfigMap(vmap.GetStringMap(sname))

	res.LoginToken = vcfg.GetString("LoginToken")
	res.UserCacheTime = vcfg.GetDuration("UserCacheTime") * time.Second
	return res
}
